package com.wuxuehua.myapp;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 吴学华
 * Date: 17-10-20
 * Time: 下午4:14
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Pay {

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    IdentityService identityService;

    @Test
    public void bs(){
        String key = "pay_test";
        String businessKey = "2345463433";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key,businessKey);
        System.out.println(processInstance.getId() + "name " +processInstance.getName());
    }

    /**
     * 流程等待支付完成
     */
    @Test
    public void pay(){

        String activityId = "waitpay";
        String id = "95001";
        Execution execution = runtimeService.createExecutionQuery().processInstanceId(id).activityId(activityId).singleResult();
        assertNotNoull(execution);
        //TODO:支付

        //支付完成

        Map<String, Object> map = new HashMap<>();
        map.put("shenman",16);
        runtimeService.signal(execution.getId(),map);
        System.out.println("支付完成 进入下一个流程");
    }

    private void assertNotNoull(Execution execution) {
        if (execution == null && !StringUtils.isNotBlank(execution.getId())){
            throw new RuntimeException("任务实例不存在");
        }
    }

    @Test
    public void geren(){
        String userId = "16";
        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.taskAssignee(userId);
        List<Task> list = taskQuery.list();
        for (Task task:list) {
            System.out.println(task.getId() + "name:" +task.getName() );
        }
    }

    @Test
    public void gruop(){
        String userId = "16";
        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.taskCandidateUser(userId);
        List<Task> list = taskQuery.list();
        for (Task task:list) {
            System.out.println(task.getId() + "name:" +task.getName() );
        }
    }

    @Test
    public void claim(){
        String taskId = "77502";
        String userId = "16";
        taskService.claim(taskId,userId);
    }

    @Test
    public void complete(){
        String taskId = "90003";
        Map<String, Object> map = new HashMap<>();
        map.put("result","0");
        taskService.complete(taskId,map);
    }




}
